#!/usr/bin/perl

my $dir = $ARGV[1];
#print $ARGV[0]."\t".$ARGV[1]."\t".$ARGV[2]."\n";
chomp $dir;
#use lib "$dir";
$dir1 = $dir."/bin/bin_HGUA/parse_cdf";
#use lib "$dir/bin";
use lib "$ARGV[1]/bin/bin_HGUA";
#use lib "/data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/NormSumSrc/HTHGU_GeneSumm_single/bin";
my $fil = $ARGV[0];
chomp $fil;

my $ascii_dir = $ARGV[2];
chomp $ascii_dir;

my $ascii_dir_out = $ascii_dir."out";

print "reading from $fil\n";
use Bio::Affymetrix::WCEL;
#use strict;
my $cel_template=new Bio::Affymetrix::WCEL;
my $cel_ht=new Bio::Affymetrix::WCEL;


open FIN,"<$dir/bin/bin_HGUA/output_hthgu_hguA.txt" or die "Can't open $dir/bin/bin_HGUA/output_hthgu_hguA.txt : $!";
#open(FIN,"<output_hthgu_hguA.txt");
my $l="";
my %MAP={};
while($l=<FIN>) {
    chomp $l;
    $l =~ s/[\r,\l,\n]+$//g;
    my @line = split(/\t/,$l);
    my $xht=$line[$#line-3];
    my $yht=$line[$#line-2];
    my $xu133=$line[$#line-1];
    my $yu133=$line[$#line];
    my @xy=($xht,$yht);
    $MAP{$xu133 . "-" . $yu133} = \@xy;
}
close FIN;

$cel_template->parse_from_file("$dir/bin/bin_HGUA/GSM245730.CEL"); # U133A template
#$cel_template->parse_from_file("GSM245730.CEL"); 
#$cel_ht->parse_from_file("/data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/samplefiles_asc/$fil"); # HT source file
$cel_ht->parse_from_file("$ascii_dir/$fil"); # HT source file
#$cel_ht->parse_from_file("GSM431121.CEL");
my $im = $cel_template->intensity_map();
my $imht =$cel_ht->intensity_map();
=head
# Print out all of the intensities for each square
my @match;
my @match_whole;
my $num_match =0;
my @mismatch;
my $num_mismatch=0;

for (my $x=0;$x<scalar(@{$cel_template->intensity_map()});$x++) {
        for (my $y=0;$y<scalar(@{$cel_template->intensity_map()->[$x]});$y++) {
	    my $key = $x . "-" . $y;
	    if(exists $MAP{$key}) {
		my $xy_ref = $MAP{$key};
		my $xht = $xy_ref->[0];
		my $yht = $xy_ref->[1];
               # $im->[$x][$y]->[0] =  $imht->[$xht][$yht]->[0] ;
               # $im->[$x][$y]->[1] =  $imht->[$xht][$yht]->[1] ;
               # $im->[$x][$y]->[2] =  $imht->[$xht][$yht]->[2] ;
		$match[$num_match] = $imht->[$xht][$yht]->[0];
		$match_whole[$num_match] = $imht->[$xht][$yht]->[0]."_".$imht->[$xht][$yht]->[1]."_".$imht->[$xht][$yht]->[2] ;
	    	$num_match++;
	    }
	    else
	   {

		$num_mismatch++;

		}
        }
}

@index = sort{ $match[ $a ] <=> $match[ $b ] } 0 .. $#match;
@match = sort {$a <=> $b}@match;
for($i=0;$i<@match_whole;$i++)
{
	$match_whole1[$i] = $match_whole[$index[$i]];

}
#$factor = $num_match/$num_mismatch;
#for($i=0;$i<$num_mismatch;$i++)
#{
#	$a = ($factor*($i+1))-1;
#       $b = sprintf("%.0f", $a);
# 	$mismatch[$i] = $match_whole1[$b];

#}
=head
my $i=0;
while($i<$num_mismatch) {
    $factor = $num_mismatch-$i;
    if($factor <0.1*(1+$#match_whole1)) {
	# Sample without replacement.. this is slow if need to sample from most of the reference distrib
	my %PICKED;
	while($i<$num_mismatch) {
	   my $indx = int(0.5+rand()*(1+$#num_mismatch));
	   if(! exists $PICKED{$indx}) {
	       $mismatch[$i] = $match_whole[$indx];
	       $i++;
	       $PICKED{$indx}=1;
	   }
	}
    } else {
	# Uniform Sampling. This is slow when we only need to sample a small fraction of the reference distribution
	for(my $b=0;($i<$num_mismatch) && ($b<=$#match_whole1);$b++) {
		my $frnd = $num_match*rand();
		if($frnd<=$factor) {
	    	$mismatch[$i]=$match_whole[$b];
	    	$i++;
		}
	}
    }
}
=cut
$i_num =0;
#print "second\n";
#print "$num_match\n$num_mismatch\n@match\n@match_whole1\n@mismatch\n";
for (my $x=0;$x<scalar(@{$cel_template->intensity_map()});$x++) {
        for (my $y=0;$y<scalar(@{$cel_template->intensity_map()->[$x]});$y++) {
            my $key = $x . "-" . $y;
            if(exists $MAP{$key}) {
                my $xy_ref = $MAP{$key};
                my $xht = $xy_ref->[0];
                my $yht = $xy_ref->[1];
                $im->[$x][$y]->[0] =  $imht->[$xht][$yht]->[0] ;
                $im->[$x][$y]->[1] =  $imht->[$xht][$yht]->[1] ;
                $im->[$x][$y]->[2] =  $imht->[$xht][$yht]->[2] ;
#		print ".";
                }
#           else
 #               {
#			@ar = split("_",$mismatch[$i_num]);
#			 $im->[$x][$y]->[0] = $ar[0];
#			$im->[$x][$y]->[1] = $ar[1];
#			$im->[$x][$y]->[2] = $ar[2];
#			$i_num++;
 #              }
        }
}


$cel_template->intensity_map($im);
$cel_template->write_to_file("$ascii_dir_out/$fil");
#$cel_template->write_to_file("HGU21.CEL");
